var moldId = getQueryString("moldId");
var moldTypeId = getQueryString("moldTypeId");
$(function () {
    vm.moldId = moldId;
    vm.moldTypeId = moldTypeId;
    $("#jqGrid").Grid({
        url: '../modulpositioncorrelation/list?moldId=' + moldId,
        rownumWidth: 35,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '备件名称', name: 'positionName', index: 'POSITION_NAME', width: 120},
            {label: '位置号', name: 'positionNo', index: 'POSITION_NO', width: 70},
            {label: '特殊处理', name: 'positionRemark', index: 'POSITION_REMARK'},
            {label: '类型', name: 'modType', index: 'MOD_TYPE', width: 60},
            {label: '寿命标准', name: 'lifeStandard', index: 'LIFE_STANDARD', width: 60},
            {label: '最近一次更换时间', name: 'changeTime', index: 'CHANGE_TIME', width: 100},
            {label: '剩余寿命', name: 'residualLife', index: 'RESIDUAL_LIFE', width: 60},
            {
                label: '当前次数', index: 'CURRENT_TIME', width: 60,
                formatter: function (value, col, row) {
                    var stan = row.lifeStandard;
                    var currentTime = row.currentTime;
                    if (currentTime == null || currentTime == "" || stan == null || stan == "") {
                        return 0;
                    }
                    var ys = stan * 0.8;
                    if (currentTime < ys) {
                        return currentTime;
                    } else if (currentTime >= ys && currentTime <= stan) {
                        return '<span style="color: orange">' + currentTime + '</span>';
                    } else {
                        return '<span style="color: red">' + currentTime + '</span>';
                    }
                }
            },
            {name: 'currentTime', hidden: true}
        ]
    });
    vm.getMoldPositionTypes();
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        modulPositionCorrelation: {
            modType: '',
            estimateNum: '',
            lifeStandard: '',
            currentTime: '',
            replaceTimes: '',
            setNum: '',
            residualLife: '',
            changeTime: '',
            positionRemark: '',
            positionName: '',
            positionNo: '', // 位置号
            spec: '', // 规格
            modulCode: '',
            barCodeRange: '', // 适用型芯条码
            id: '',
        },
        ruleValidate: {
            positionName: [
                {required: true, message: '备件名称必填', trigger: 'blur'}
            ],
            positionNo: [
                {required: true, message: '位置号必填', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            positionNo: '', // 位置号
            modType: ''
        },
        moldId: '', // 模具ID
        moldTypeId: '',
        moduleTypeList: enumsToList('MODULE_SPAREPART'), //模具 备件更换 镶块、型芯、镶件
        moldPositionTypes: []
    },
    methods: {
        /**
         * 查看模具详情
         */
        managePosition: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 模具类型下位置关联',
                content: '../module/moldTypePositionForSelect.html?moldTypeId=' + this.moldTypeId,
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var selectedPos = iframeWin.getSelectedPos();
                    if (!selectedPos) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        vm.modulPositionCorrelation.positionName = selectedPos.positionName;
                        vm.modulPositionCorrelation.positionNo = selectedPos.positionNo;
                        vm.modulPositionCorrelation.spec = selectedPos.spec;
                        vm.modulPositionCorrelation.modType = selectedPos.modType;
                        vm.modulPositionCorrelation.positionRemark = selectedPos.positionRemark;
                        vm.modulPositionCorrelation.lifeStandard = selectedPos.lifeStandard;
                        vm.modulPositionCorrelation.moldTypePosId = selectedPos.id;

                        layer.close(index);
                    }
                }
            })
        },
        getMoldPositionTypes: function () {
            // 获取备件单位
            Ajax.request({
                url: '../modulpositioncorrelation/listUnit',
                async: false,
                successCallback: function (r) {
                    vm.moldPositionTypes = r.moldPositionTypes;
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.modulPositionCorrelation = {
                modType: '',
                estimateNum: '',
                lifeStandard: '',
                currentTime: '',
                replaceTimes: '',
                setNum: '',
                residualLife: '',
                changeTime: new Date(),
                positionRemark: '',
                positionName: '',
                positionNo: '', // 位置号
                spec: '', // 规格
                barCodeRange: '',
                moldId: moldId,
                id: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.modulPositionCorrelation.id == (null || '') ? "../modulpositioncorrelation/save" : "../modulpositioncorrelation/update";
            // 处理下换件时间格式
            if(vm.modulPositionCorrelation.changeTime){
                vm.modulPositionCorrelation.changeTime = gmt2DateMinuteStr(vm.modulPositionCorrelation.changeTime)
            }
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.modulPositionCorrelation),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulpositioncorrelation/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulpositioncorrelation/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.modulPositionCorrelation = r.modulPositionCorrelation;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            // 构建查询参数
            var param = {};
            if (vm.q.name) {
                param.name = vm.q.name;
            }
            if (vm.q.modType) {
                param.modType = vm.q.modType;
            }
            if (vm.q.positionNo) {
                param.positionNo = vm.q.positionNo;
            }
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                positionNo: '', // 位置号
                modType: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        goModuleReplacePart: function (modulCode) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-pencil"></i> 更换记录',
                content: '../module/modulereplacepart.html?id=' + '&modulCode=' + modulCode
            })
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles();
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (res.code == 0) {
                this.$Loading.finish();
                alert(res.msg, function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
        convertGMT2Year: function (time) {
            return new Date(time).getFullYear();
        },
        //数据导出
        exportData: function () {
            window.open('../modulpositioncorrelation/export?moldId=' + this.moldId);
        }
    }
});
